package hyl.base.flow;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import hyl.core.MyFun;

/**
 * 流模版 流编号 在外部定义 String flowid=MyFun.getUUID();
 * 
 */
public class Flow {
	public String _name = null;
	protected PoolFlow _pf;
	// 所有相关程序环节
	private Map<String, Link> _links = new HashMap<>();
	/** 流中 状态的键值对集合 */
	private List<String> _trackkeys = new ArrayList<String>();
	/** 流状态的默认值集合 */
	private StringBuilder _trkDefMarks = new StringBuilder();

	/*
	 * public void commit() { if (_pf!=null) _pf.commit(); }
	 */
	private void loadMarks(String keys) {
		if (MyFun.isEmpty(keys))
			return;
		decodeTracks(keys, Track.UNABLE);
	}

	/** 得到状态字段集合,按先后排列 */
	public List<String> getStateList() {
		return _trackkeys;
	}

	/** 得到状态字段默认值,char[]按先后排列 */
	public String getDefVals() {
		return _trkDefMarks.toString();
	}

	/** 得到某个字段在状态字段集合中的位置 */
	public int getIndex(String state) {
		return _trackkeys.indexOf(state);
	}

	/**
	 * 新建一条flow 流水,<br>
	 * 新建以后的Tracks 为初始化值<br>
	 * 如果数据库中有,直接从数据库中载入,如果数据库中没有就用默认值填充.
	 * 
	 */
	public Track getTrkInstance(String flowid) {
		/** 导出状态的键值对集合 */
		return _pf.getFlowDao().getTrack(flowid, this);
	}

	public Flow(PoolFlow pf, String name, String tracks, Object links) {
		_name = name;
		_pf = pf;
		loadMarks(tracks);
		if (links == null)
			return;
		JSONArray li = (JSONArray) links;
		for (int i = 0; i < li.size(); i++) {
			JSONObject link = li.getJSONObject(i);
			String a = link.getString("name");
			String b = link.getString("depend");
			// JSONObject b2= link.getJSONObject("depend2");
			Object c = link.get("result");
			// JSONObject c2= link.getJSONObject("result2");
			Object d = link.get("error");
			// JSONObject d2= link.getJSONObject("error2");
			Integer e = li.getJSONObject(i).getInteger("retry");
			Link wn = new Link(this, a, b, c, d, e);
			_links.put(a, wn);
		}
	}

	/**
	 * 返回模型的名称
	 */
	public String getName() {
		return _name;
	}

	/**
	 * 解析 流状态表达式
	 */
	private void decodeTracks(String express, Character defchar) {
		// 解析表达式 f=0 开始, =1 遇到'='号 , =2 遇到 ' '符
		int f = 0;
		StringBuilder field = new StringBuilder();
		Character value = null;
		for (int i = 0; i < express.length(); i++) {
			char c = express.charAt(i);
			if (c == '=') {// 遇到等号跳过
				f = 1;
			} else if (c == ',') {
				if (f < 2) {
					_trackkeys.add(field.toString());
					if (value == null)
						value = defchar;
					_trkDefMarks.append(value);
				}
				f = 0;
				field.setLength(0);
				value = null;
			} else if (c == ' ') {
				continue;
			} else {
				if (f == 0) {// 意味在 =号左边
					field.append(c);
				} else if (f == 1) {// 遇到等号
					if (field.length() == 0) // 没有字段跳过
						continue;
					_trackkeys.add(field.toString());
					if (value == null)
						value = defchar;
					_trkDefMarks.append(value);
					f = 2;
				}
			}
		}
		if (f < 2) {
			_trackkeys.add(field.toString());
			if (value == null)
				value = defchar;
			_trkDefMarks.append(value);
		}
	}

	/**
	 * @return the _nodes
	 */
	public Link getLink(String key) {
		return _links.get(key);
	}

}
